import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/modules/cabinet/api/cabinetApi.dart';
import 'package:honeypotflutter/public/public_total.dart';
import '../../model/cabinet_info_model.dart';
import '../../model/cabinet_info_status_model.dart';

class CabinetListInfoEditLogic extends GetxController {
  /// 禁用启用
  RxBool isAction = RxBool(true);

  /// 输入框
  TextEditingController controller = TextEditingController();

  late CabinetInfoModel infoModel;
  CabinetInfoStatusModel? statusModel;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    Map<String, dynamic> arg = Get.arguments;
    infoModel = arg["info"];
    statusModel = arg["status"];
    controller.text = statusModel?.courierPhone ?? "";
    isAction.value = (statusModel != null && statusModel!.isActive == true) ? true : false;
  }

  /*
  * 确认
  * */
  sureAction() async {
    if (!GetUtils.isPhoneNumber(controller.text)) {
      HLTotal.total("请输入正确手机号码");
      return;
    }
    if (statusModel == null) {
      await CabinetApi.createCabinetBind(
        boardNo: infoModel.boardNo,
        displayNo: infoModel.tmcDevices?.disPlayNo,
        courierPhone: controller.text,
        isActive: isAction.value,
      );
      Get.back(result: true);
      return;
    }
    await CabinetApi.changeBind(
      id: statusModel!.id!,
      courierPhone: controller.text,
      isActive: isAction.value,
    );
    Get.back(result: true);
  }
}
